package com.asurplus.system.service.impl;

import com.asurplus.common.layui.LayuiTableResult;
import com.asurplus.common.minio.MinioConst;
import com.asurplus.common.minio.MinioUtils;
import com.asurplus.common.utils.ResponseResult;
import com.asurplus.system.entity.SysFiles;
import com.asurplus.system.mapper.SysFilesMapper;
import com.asurplus.system.service.SysFilesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统附件表 服务实现类
 * </p>
 *
 * @author lizhou
 * @since 2021-01-11
 */
@Service
public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFiles> implements SysFilesService {

    @Autowired
    private MinioUtils minioUtils;

    @Override
    public LayuiTableResult list(Integer page, Integer limit, SysFiles sysFiles) {
        LambdaQueryWrapper<SysFiles> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sysFiles.getName())) {
            queryWrapper.like(SysFiles::getName, sysFiles.getName());
        }
        if (StringUtils.isNotBlank(sysFiles.getUrl())) {
            queryWrapper.like(SysFiles::getUrl, sysFiles.getUrl());
        }
        if (StringUtils.isNotBlank(sysFiles.getSuffix())) {
            queryWrapper.like(SysFiles::getSuffix, sysFiles.getSuffix());
        }
        queryWrapper.orderByDesc(SysFiles::getCreateTime);
        return new LayuiTableResult(this.baseMapper.selectPage(new Page<>(page, limit), queryWrapper));
    }

    @Override
    public ResponseResult add(SysFiles sysFiles) {
        if (null == sysFiles) {
            return ResponseResult.error("数据错误");
        }
        if (StringUtils.isBlank(sysFiles.getUrl())) {
            return ResponseResult.error("请上传附件");
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult update(SysFiles sysFiles) {
        if (null == sysFiles || null == sysFiles.getId() || 0 == sysFiles.getId()) {
            return ResponseResult.error("数据错误");
        }
        if (StringUtils.isBlank(sysFiles.getUrl())) {
            return ResponseResult.error("请上传附件");
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (null == id || 0 == id) {
            return ResponseResult.error("数据错误");
        }
        SysFiles sysFiles = this.baseMapper.selectById(id);
        if (null == sysFiles) {
            return ResponseResult.error("数据错误");
        }
        // 删除文件服务器文件
        ResponseResult responseResult = minioUtils.removeObject(MinioConst.MINIO_BUCKET, sysFiles.getUrl());
        // 删除成功
        if (200 == responseResult.getCode()) {
            // 删除数据库数据
            this.baseMapper.deleteById(id);
        }
        return responseResult;
    }
}
